﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AyaWo.Commen;
using Dapper;

namespace Qbqqban
{
    public class BlogEasay
    {
        [Key]
        public string easayid { get; set; }
        public string title { get; set; }
        public string man { get; set; }
        public string content { get; set; }
        public string img { get; set; }
        public string columnid { get; set; }
        public string sourcename { get; set; }
        public int readcount { get; set; }
        public string lablesid { get; set; }
        public int call { get; set; }
        public int disable { get; set; }
        public int tuijian { get; set; }
        public string CreatedBy { get; set; }
        public DateTime CreatedDate { get; set; }
        public string ModifiedBy { get; set; }
        public DateTime ModifiedDate { get; set; }
        public int Deleteflag { get; set; }
        public string Rowversion { get; set; }
        public string ptypeID { get; set; }
    }

    public class ContentEasay
    {
        public string title { get; set; }
        public string man { get; set; }
        public string content { get; set; }
        public string columnid { get; set; }
        public string sourcename { get; set; }
        public string ptypeID { get; set; }
        public string CreatedBy { get; set; }
        public DateTime CreatedDate { get; set; }
    }

    public static class BlogEasayBll
    {
        public static void AddCount(ContentEasay ce)
        {
            BlogEasay be = new Qbqqban.BlogEasay();
            #region ce
            be.title = ce.title;
            be.man = ce.man??"";
            be.content = ce.content;
            be.columnid = ce.columnid??"";
            be.sourcename = ce.sourcename??"";
            be.ptypeID = ce.ptypeID;
            be.CreatedBy = ce.CreatedBy;
            be.CreatedDate = ce.CreatedDate;
            #endregion

            be.easayid = CommonHelper.CreateNo();
            be.readcount = new Random().Next(10, 500);
            be.call = new Random().Next(10, 500);
            be.disable = 0;
            be.tuijian = 0;
            be.Deleteflag = 0;
            //string sqlStr = "INSERT into blogeasay(easayid,title,man,content,columnid,sourcename,readcount,call,disable,tuijian,CreatedBy,CreatedDate,Deleteflag,ptypeID) VALUES('" + be.easayid + "','" + be.title + "','" + be.man + "',@content,'" + be.columnid + "'," + be.sourcename + "," + be.readcount + "," + be.call + "," + be.disable + "," + be.tuijian + ",'" + be.CreatedBy + "',@CreatedDate," + be.Deleteflag + ",'" + be.ptypeID + "')";
            //BaseService.Db.ExecuteBySql(new StringBuilder(sqlStr), be);
            string sqlStr = "INSERT into blogeasay(easayid,title,man,content,columnid,sourcename,readcount,`call`,`disable`,tuijian,CreatedBy,CreatedDate,Deleteflag,ptypeID) VALUES(@easayid,@title,@man,@content,@columnid,@sourcename,@readcount,@call,@disable,@tuijian,@CreatedBy,@CreatedDate,@Deleteflag,@ptypeID)";
            BaseService.Db.ExecuteBySql(new StringBuilder(sqlStr), be);

        }

        public static void Add(this ContentEasay ce)
        {
            AddCount(ce);
        }

        public static bool CanInserts(string title, int? typ)
        {
            try
            {
                var q = BaseService.Db.GetDataTableBySqL(new StringBuilder("select ID from blogeasay t where t.title like @title AND ptypeID=@ptypeID "), new { title = "%" + title + "%", ptypeID = typ });
                if (q == null || q.Rows.Count == 0)
                    return true;
                else return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
